<?php
namespace app\admin\controller;
use think\Controller;
class Login extends Controller
{

	//用户登录验证
    public function index()
    {
    	if(request() -> isPost()){
    		$username = trim(input('post.username'));
    		$password = trim(input('post.password'));
    		$user = db('ucenter_members')->field('uid,username,password,salt,cate')->where('username',$username)->find();
    		if(!$user){
    			return json(['code' => 0, 'msg' => '用户名错误']);
    		}
    	    $passwords = md5(md5($password).$user['salt']);
    	    if($user['password'] != $passwords){
    	    	return json(['code' => 0, 'msg' => '密码错误']);
    	    }
    	    if($user['cate'] != 1){
    	    	return json(['code' => 0, 'msg' => '你没有权限,无法进行登录!']);
    	    }else{
    	    	session('uid',$user['uid']);
    	    	session('username',$user['username']);
    	    	return json(['code' => 1, 'msg' => '登录成功!','url' => url('index/index')]);
    	    }
    	}else{  		
            return $this->fetch();
    	}
    }

    //后台管理用户退出
	public function loginout()
	{
		// 清除session (当前作用域)
		session(null);
		$this->redirect('admin/login/index');
	}
}